package experimental;

import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import org.mitre.jawb.io.SgmlDocument;

public class SGMLtoText {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		
		String directoryName = "/home/pjbarrio/Dataset/ACE2003/ace_phase2/data/ace2_devtest/test/";
		
		File directory = new File(directoryName);
		
		File[] files = directory.listFiles();
		
		for (File file : files) {
			
			if (isSGML(file)){
				
				String text = getTextfromSGML(file);
				
				saveText(text,getTextFileName(file));
				
			}
			
		}
		
	}

	private static void saveText(String text, String fileName) throws IOException {

		BufferedWriter bw = new BufferedWriter(new FileWriter(new File(fileName)));
		
		bw.write(text);
		
		bw.close();
		
	}

	private static String getTextFileName(File file) {
		
		String fileName = file.getAbsolutePath();
		
		fileName = fileName.replaceAll(".sgml", "");
		
		fileName = fileName.replaceAll(".sgm", "");
		
		return fileName + ".txt";
		
	}

	private static String getTextfromSGML(File file) throws FileNotFoundException, IOException {
		
		SgmlDocument sgml = new SgmlDocument(new FileReader(file));
		
		return sgml.getSignalText();
		
//		String textwithTags = readFile(file);
//		
//		String noTags = textwithTags.toString().replaceAll("\\<.*?>","");
		
/*		
		
		<DOC>
		<DOCNO> 9801.35 </DOCNO>
		<DOCTYPE SOURCE="newspaper"> NEWS STORY</DOCTYPE>
		<HEADER>
		SOURCE: The Washington Post
		SECTION: A17;
		LENGTH: 518
		DATE:  January 29, 1998  
		HEADLINE:  Senate Confirms 3 Judicial Nominees as Backlog Fight Persists  
		BODY_LEN: 488
		</HEADER>
		<TEXT>

		</TEXT>
		</DOC>

*/		
		
//		return noTags;
	}

	private static String readFile(File file) {
		
	    byte[] buffer = new byte[(int) file.length()];
	    BufferedInputStream f = null;
	    try {
	        f = new BufferedInputStream(new FileInputStream(file.getAbsolutePath()));
	        f.read(buffer);
	    } catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
	        if (f != null) try { f.close(); } catch (IOException ignored) { }
	    }
	    return new String(buffer);
		
	}

	private static boolean isSGML(File file) {
		
		return (file.getName().endsWith("sgm")) || (file.getName().endsWith("sgml"));
		
	}

}
